<?php
namespace User\Controller;

use Common\Controller\AdminbaseController;

class GoodsController extends AdminbaseController {
    public function __construct(){
        parent::__construct();
    }
    //商品管理 列表
    public function goods_list(){
        $request = I( 'request.');
        $where=[];
        if(!empty($request['title'])){
            $where['title']=['like','%'.$request['title'].'%'];
        }
        $goods_model=M( "goods", 'st_', C( 'DB_CONFIG2' ) );
        $count = $goods_model->where( $where )->count();
        $page = $this->page( $count, 20 );
        $fields_search='id,title,amount,crtime,startime,endtime,status,interest,examine,consulting,bond_amount,modtime';
        $list = $goods_model->where( $where )
            ->order( 'crtime desc' )
            ->limit( $page->firstRow . ',' . $page->listRows )
            ->field( $fields_search )
            ->select();
        //首页配置
        $home_pages=M( "home_page", 'st_', C( 'DB_CONFIG2' ) )->field(['goods_id'])->select();
        $home_page=[];
        foreach($home_pages as $v){
            $home_page[]=$v['goods_id'];
        }
        $this->assign( 'home_page', $home_page );
        $this->assign( 'list', $list );
        $this->assign( "page", $page->show( 'Admin' ) );
        $this->display();
    }
    /*
     * 编辑商品列表
     */
    public function goods_edit(){
        $request = I( 'request.');
        $list=[];
        if(!empty($request['id'])) {
            $goods_model=M( "goods", 'st_', C( 'DB_CONFIG2' ) );
            $goods_url_model=M( "goods_url", 'st_', C( 'DB_CONFIG2' ) );
            $fields_search='id,amount,interest,examine,consulting,color,spec,startime,endtime,title,subtitle,comm_id,bond_amount';
            $list=$goods_model->field($fields_search)->where(['id'=>$request['id']])->find();
            if(!empty($list['id'])){
                $url=$goods_url_model->field('id,url,type')->where(['goods_id'=>$list['id'],'status'=>1])->select();
                $list['url']=$url;
            }
        }
        $this->assign( 'list', $list );
        $this->assign( 'commodity', $this->_commodity() );
        $this->assign( 'amount_list', $this->_amount() );
        $this->display();
    }
    /*
     * 编辑提交商品
     */
    public function goods_edit_act(){
        $request = I( 'request.');
        if(empty($request['amount_id'])){
            $this->error('请选择额度');
        }
        $amount=$this->_amount($request['amount_id']);
        $data=[
            'title'=>empty($request['title'])?'':$request['title'],
            'subtitle'=>empty($request['subtitle'])?'':$request['subtitle'],
            'comm_id'=>empty($request['comm_id'])?'':$request['comm_id'],//分类
            'amount'=>$amount['amount'],//额度
            'interest'=>$amount['interest'],
            'examine'=>$amount['examine'],
            'consulting'=>$amount['consulting'],
            'bond_amount'=>$amount['bond_amount'],
            'color'=>empty($request['color'])?'':$request['color'],//颜色
            'spec'=>empty($request['spec'])?'':$request['spec'],//规格
            'startime'=>empty($request['startime'])?1:$request['startime'],
            'endtime'=>empty($request['endtime'])?1:$request['endtime'],
            'modtime'=>time(),
        ];
//        if(in_array('',$data)){
//            $this->error('请填写商品信息');
//        }
        $goods_model=M( "goods", 'st_', C( 'DB_CONFIG2' ) );
        $goods_url_model=M( "goods_url", 'st_', C( 'DB_CONFIG2' ) );
        if(!empty($request['id'])) {
            $goods_id=$request['id'];
            $goods_model->where(['id'=>$goods_id])->save($data);
            $msg='商品编辑成功!';
        }else{
            $goods_model->add($data);
            $goods_id=$goods_model->getlastInsID();
            $msg='商品添加成功!';
        }
        if(!empty($request['photos_url'])){//banner图
            $num=$goods_url_model->where(['goods_id'=>$goods_id,'type'=>2])->count();
            if($num>0){
                $goods_url_model->where(['goods_id'=>$goods_id,'type'=>2])->delete();
            }
            $insert=[];
            foreach($request['photos_url'] as $v){
                $insert[]=[
                    'goods_id'=>$goods_id,
                    'type'=>'2',
                    'url'=>$v,
                ];
            }
            if(!empty($insert)){
                $goods_url_model->addAll($insert);
            }
        }
        if(!empty($request['pbc_url'])){//详情图
            $num=$goods_url_model->where(['goods_id'=>$goods_id,'type'=>3])->count();
            if($num>0){
                $goods_url_model->where(['goods_id'=>$goods_id,'type'=>3])->delete();
            }
            $insert=[];
            foreach($request['pbc_url'] as $v){
                $insert[]=[
                    'goods_id'=>$goods_id,
                    'type'=>'3',
                    'url'=>$v,
                ];
            }
            if(!empty($insert)){
                $goods_url_model->addAll($insert);
            }
        }
        $this->success($msg,U( "Goods/goods_list" ));
    }
    /*
     * 首页商品加入
     */
    public function join_hpage(){
        $request = I( 'request.');
        if(empty($request['id']) || !is_numeric($request['id'])){
            $this->error('请刷新重试');
        }
        $goods_model=M( "goods", 'st_', C( 'DB_CONFIG2' ) );
        $goods=$goods_model->field('id,status')->where(['id'=>$request['id']])->find();
        if(empty($goods['id'])){
            $this->error('该商品不存在!');
        }
        if(empty($goods['status'])){//启用
            $status=1;
            $msg='启用';
        }else{
            $status=0;
            $msg='关闭';
        }
        try{
            $goods_model->where(['id'=>$request['id']])->save(['status'=>$status]);
        }catch(Exception $e){
            $this->error($msg.'失败!');
        }
        $this->success($msg.'成功!');
    }
    //商品分类 列
    public function _commodity(){
        $dt=[];
        $data=M( "goods_commodity", 'st_', C( 'DB_CONFIG2' ))->field('id,name')->select();
        foreach($data as $v){
            $dt[$v['id']]=$v['name'];
        }
        return $dt;
    }
    /*
     * 可使用 额度
     * 查询当前可使用的额度配置
     */
    public function _amount($id=''){
        $model=M( "amount", 'system_', C( 'DB_CONFIG2' ));
        if(!empty($id)){
            $data=$model->field('id,name,amount,interest,examine,consulting,bond_amount')
                ->where(['status'=>1,'id'=>$id])
                ->find();
            if(empty($data)){
                $data=[
                    'amount'=>'1400',
                    'interest'=>'100',
                    'examine'=>'150',
                    'consulting'=>'150',
                    'bond_amount'=>'200',
                ];
            }
        }else{
            $data=$model->field('id,name,amount,interest,examine,consulting,bond_amount')
                ->where(['status'=>1])
                ->select();
        }
        return $data;
    }
    /*
     * 商品状态启用关闭
     */
    public function check_type(){
        $request = I( 'request.');
        if(empty($request['id']) || !is_numeric($request['id'])){
            $this->error('请刷新重试');
        }
        $goods_model=M( "goods", 'st_', C( 'DB_CONFIG2' ) );
        $goods=$goods_model->field('id,status')->where(['id'=>$request['id']])->find();
        if(empty($goods['id'])){
            $this->error('该商品不存在!');
        }
        if(empty($goods['status'])){//启用
            $status=1;
            $msg='启用';
        }else{
            $status=0;
            $msg='关闭';
        }
        try{
            $goods_model->where(['id'=>$request['id']])->save(['status'=>$status]);
        }catch(Exception $e){
            $this->error($msg.'失败!');
        }
        $this->success($msg.'成功!');
    }
    /*
     * 首页轮播设置
     * 不分页
     */
    public function hpage_list(){
        $where=[
            'a.type'=>1,
        ];
        $fields_search='a.id,a.sort,a.goods_id,a.jump,a.crtime,a.url,b.title,a.status';
        $goods_url_model=M( "goods_url a", 'st_', C( 'DB_CONFIG2' ) );
        $list = $goods_url_model->where( $where )
            ->join('LEFT JOIN st_goods b on a.goods_id =b.id')
            ->order( 'a.sort desc,a.id desc' )
            ->field( $fields_search )
            ->select();
        $this->assign( 'list', $list );
        $this->display();
    }
    /*
     * 首页图片详情
     */
    public function hpage_edit(){
        $request = I( 'request.');
        $list=[];
        if(!empty($request['id'])) {
            $goods_url_model=M( "goods_url", 'st_', C( 'DB_CONFIG2' ) );
            $fields_search='id,goods_id,jump,url';
            $list=$goods_url_model->field($fields_search)->where(['id'=>$request['id']])->find();
        }
        $this->assign( 'list', $list );
        $this->display();
    }
    /*
     * 首页图片编辑提交
     */
    public function hpage_edit_act(){
        $request = I( 'request.');
        if(empty($request['thumb'])){
            $this->error('请选择图片!');
        }
        $data=[
            'url'=>$request['thumb'],
            'goods_id'=>empty($request['goods_id'])?0:$request['goods_id'],
            'jump'=>empty($request['jump'])?'':$request['jump'],
            'type'=>1,
            'from'=>0,
            'crtime'=>time(),
        ];
        $goods_url_model=M( "goods_url", 'st_', C( 'DB_CONFIG2' ) );
        if(!empty($request['id']) && is_numeric($request['id']))
            $msg='编辑';
        else
            $msg='添加';
        try{
            if(!empty($request['id']) && is_numeric($request['id'])){
                $goods_url_model->where(['id'=>$request['id']])->save($data);
            }else{
                $goods_url_model->add($data);
            }
        }catch(Exception $e){
            $this->error($msg.'失败!');
        }
        $this->success($msg.'成功!',U( "Goods/hpage_list" ));
    }
    /*
     * 首页状态启用关闭
     */
    public function check_type_hp(){
        $request = I( 'request.');
        if(empty($request['id']) || !is_numeric($request['id'])){
            $this->error('请刷新重试');
        }
        $goods_url_model=M( "goods_url", 'st_', C( 'DB_CONFIG2' ) );
        $goods=$goods_url_model->field('id,status')->where(['id'=>$request['id'],'type'=>1])->find();
        if(empty($goods['id'])){
            $this->error('该首页图片不存在!');
        }
        if(empty($goods['status'])){//启用
            $status=1;
            $msg='启用';
        }else{
            $status=0;
            $msg='关闭';
        }
        try{
            $goods_url_model->where(['id'=>$request['id']])->save(['status'=>$status]);
        }catch(Exception $e){
            $this->error($msg.'失败!');
        }
        $this->success($msg.'成功!');
    }
    /*
     * 首页 排序
     */
    public function hp_sort(){
        $request = I( 'request.');
        if(empty($request['listorders'])){
            $this->error('请刷新重试');
        }
        $when=[];
        $ids=[];
        foreach($request['listorders'] as $k=>$v){
            $ids[]=$k;
            $when[]='WHEN '.$k.' THEN '.$v;
        }
        $goods_url_model=M( "goods_url", 'st_', C( 'DB_CONFIG2' ) );
        try{
            $sql='UPDATE st_goods_url SET sort =CASE id '.implode(' ',$when).' END WHERE id IN ('.implode(',',$ids).')';
            $goods_url_model->execute($sql);
        }catch(Exception $e){
            $this->error('排序失败!');
        }
        $this->success('排序成功!');
    }
    /*
     * 租赁设置
     */
    public function site(){
        $site_model=M( "string", 'st_', C( 'DB_CONFIG2' ) );
        $list=$site_model->where('type>1')->field('id,string,type,url')->select();
        $this->assign( 'list', $list );
        $this->display();
    }
    public function site_act(){
        $request = I( 'request.options');
        if(in_array('',$request)){
            $this->error('设置参数不能为空!');
        }
        $string=[];
        $url=[];
        foreach($request as $k=>$v){
            switch ($k){
                case 1:
                case 2:
                case 5:
                    $string[]= 'WHEN '.$k.' THEN "'.$v.'"';
                    $url[]= 'WHEN '.$k.' THEN null';
                    break;
                case 3:
                case 4:
                    $string[]= 'WHEN '.$k.' THEN null';
                    $url[]= 'WHEN '.$k.' THEN "'.$v.'"';
                    break;
            }
        }
        $site_model=M( "string", 'st_', C( 'DB_CONFIG2' ) );
        try{
            $sql='UPDATE st_string SET string = CASE id '.implode(' ',$string).' END ,url = CASE id '.implode(' ',$url).' END where id in(1,2,3,4,5)';
            $site_model->execute($sql);
        }catch(Exception $e){
            $this->error('设置保存失败!');
        }
        $this->success('设置保存成功!',U( "Goods/site" ));
    }
    /*
     * APP更新设置
     */
    public function app_site(){
        $site_model=M( "string", 'st_', C( 'DB_CONFIG2' ) );
        $list=$site_model->where(['type'=>1])->field('id,string,type,url,mobile_type,is_up')->select();
        $this->assign( 'list', $list );
        $this->display();
    }
    /*
     *认证设置
     */
    public function authentication(){
        $auth_model=M( "authentication", 'st_', C( 'DB_CONFIG2' ) );
        $list=$auth_model->field('id,title,status,is_must,string,mod,sort')->order('')->select();
        $this->assign( 'list', $list );
        $this->display();
    }
}
